package com.wl.dzt.sys.core.dao.select.term;


/**
 * @author wl_code@163.com
 * @version 1.0
 * @date 2018/7/5/005
 * @description 查询条件
 * 查询条件的封装
 */
public class Term {

    /**
     * 字段
     */
    private String field;

    /**
     * 条件
     */
    private String op;

    /**
     * 值
     */
    private Object value;

    /**
     * 条件分组
     */
    private String group;

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }

    public String getOp() {
        return op;
    }

    public void setOp(String op) {
        this.op = op;
    }

    /**
     * <b>获取值</b>
     * <p>
     * 如果存在in则自动进行适配的转换
     * </p>
     *
     * @return
     */
    public Object getValue() {
        if ((this.op.equalsIgnoreCase("in") || this.op.equalsIgnoreCase("!in")) && this.value instanceof String) {
            String data[] = value.toString().split(",");
            if (data.length >= 1) {
                return data;
            }
        }
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    /**
     * <b>等于</b>
     * <p>
     * 等于
     * </p>
     *
     * @param field
     * @param value
     * @return
     */
    public static Term eq(String field, Object value) {
        Term term = new Term();
        term.setField(field);
        term.setOp("=");
        term.setValue(value);
        return term;
    }

    /**
     * <b>like</b>
     * <p>
     * like
     * </p>
     *
     * @param field
     * @param value
     * @return
     */
    public static Term like(String field, String value) {
        Term term = new Term();
        term.setField(field);
        term.setOp("like");
        term.setValue(value);
        return term;
    }

    /**
     * <b>小于</b>
     * <p>
     * 小于
     * </p>
     *
     * @param field
     * @param value
     * @return
     */
    public static Term lt(String field, Object value) {
        Term term = new Term();
        term.setField(field);
        term.setOp("<");
        term.setValue(value);
        return term;
    }

    /**
     * <b>小于等于 </b>
     * <p>
     * 小于等于
     * </p>
     *
     * @param field
     * @param value
     * @return
     */
    public static Term lq(String field, Object value) {
        Term term = new Term();
        term.setField(field);
        term.setOp("<=");
        term.setValue(value);
        return term;
    }

    /**
     * <b> 大于</b>
     * <p>
     * 大于
     * </p>
     *
     * @param field
     * @param value
     * @return
     */
    public static Term gt(String field, Object value) {
        Term term = new Term();
        term.setField(field);
        term.setOp(">");
        term.setValue(value);
        return term;
    }

    /**
     * <b>大于等于</b>
     * <p>
     * 大于等于
     * </p>
     *
     * @param field
     * @param value
     * @return
     */
    public static Term gq(String field, Object value) {
        Term term = new Term();
        term.setField(field);
        term.setOp(">=");
        term.setValue(value);
        return term;
    }

    /**
     * <b>in</b>
     * <p>
     * in
     * </p>
     *
     * @param field
     * @param values
     * @return
     */
    public static Term in(String field, String... values) {
        Term term = new Term();
        term.setField(field);
        term.setOp("in");
        term.setValue(values);
        return term;
    }

    /**
     * <b> 为空</b>
     * <p>
     * 为空
     * </p>
     *
     * @param field
     * @return
     */
    public static Term isNull(String field) {
        Term term = new Term();
        term.setField(field);
        term.setOp("isNull");
        return term;
    }

    /**
     * <b>不等于</b>
     * <p>
     * 不等于
     * </p>
     *
     * @param field
     * @param value
     * @return
     */
    public static Term notEq(String field, Object value) {
        Term term = new Term();
        term.setField(field);
        term.setOp("!=");
        term.setValue(value);
        return term;
    }

    /**
     * <b>不模糊匹配</b>
     * <p>
     * 不模糊匹配
     * </p>
     *
     * @param field
     * @param value
     * @return
     */
    public static Term notLike(String field, Object value) {
        Term term = new Term();
        term.setField(field);
        term.setOp("!like");
        term.setValue(value);
        return term;
    }

    /**
     * <b>not in</b>
     * <p>
     * not in
     * </p>
     *
     * @param field
     * @param values
     * @return
     */
    public static Term notIn(String field, String... values) {
        Term term = new Term();
        term.setField(field);
        term.setOp("!in");
        term.setValue(values);
        return term;
    }

    /**
     * <b>不为空</b>
     * <p>
     * 不为空
     * </p>
     *
     * @param field
     * @return
     */
    public static Term notNull(String field) {
        Term term = new Term();
        term.setField(field);
        term.setOp("!isNull");
        return term;
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

}
